using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using JGSY.CMS.LowCode.Platform.Domain.Interfaces;

namespace JGSY.CMS.LowCode.Platform.Domain.Entities
{
    /// <summary>
    /// 页面管理实体
    /// 对应 pages 表，支持层级结构和模板系统
    /// </summary>
    [Table("pages")]
    public class Page : ITenantEntity
    {
        /// <summary>
        /// 租户ID
        /// </summary>
        [Required]
        [StringLength(36)]
        [Column("tenant_id")]
        public string TenantId { get; set; } = string.Empty;

        /// <summary>
        /// 主键ID
        /// </summary>
        [Key]
        public long Id { get; set; }
        
        /// <summary>
        /// 页面标题
        /// </summary>
        [Required]
        [StringLength(255)]
        public string Title { get; set; } = string.Empty;
        
        /// <summary>
        /// 页面别名/URL
        /// </summary>
        [Required]
        [StringLength(255)]
        public string Slug { get; set; } = string.Empty;
        
        /// <summary>
        /// 父页面ID（层级结构）
        /// </summary>
        public long? ParentId { get; set; }
        
        /// <summary>
        /// 关联模板ID
        /// </summary>
        public long? TemplateId { get; set; }
        
        /// <summary>
        /// 页面内容（富文本/JSON）
        /// </summary>
        [Column(TypeName = "text")]
        public string? Content { get; set; }
        
        /// <summary>
        /// 页面描述/摘要
        /// </summary>
        [StringLength(500)]
        public string? Description { get; set; }
        
        /// <summary>
        /// 页面标签（JSON格式）
        /// </summary>
        [Column(TypeName = "json")]
        public string? Tags { get; set; }
        
        /// <summary>
        /// SEO标题
        /// </summary>
        [StringLength(255)]
        public string? SeoTitle { get; set; }
        
        /// <summary>
        /// SEO关键词
        /// </summary>
        [StringLength(500)]
        public string? SeoKeywords { get; set; }
        
        /// <summary>
        /// SEO描述
        /// </summary>
        [StringLength(500)]
        public string? SeoDescription { get; set; }
        
        /// <summary>
        /// 自定义属性（JSON格式）
        /// </summary>
        [Column(TypeName = "json")]
        public string? CustomFields { get; set; }
        
        /// <summary>
        /// 排序权重
        /// </summary>
        public int SortOrder { get; set; } = 0;
        
        /// <summary>
        /// 访问量
        /// </summary>
        public int ViewCount { get; set; } = 0;
        
        /// <summary>
        /// 是否在导航中显示
        /// </summary>
        public bool ShowInNavigation { get; set; } = false;
        
        /// <summary>
        /// 状态（草稿/发布等）
        /// 0=草稿, 1=发布, 2=归档
        /// </summary>
        public byte Status { get; set; }
        
        /// <summary>
        /// 版本号
        /// </summary>
        public int Version { get; set; } = 1;
        
        /// <summary>
        /// 发布时间
        /// </summary>
        public DateTime? PublishTime { get; set; }
        
        /// <summary>
        /// 创建人
        /// </summary>
        [Required]
        public long CreatedBy { get; set; }
        
        /// <summary>
        /// 修改人
        /// </summary>
        public long? UpdatedBy { get; set; }
        
        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
        
        /// <summary>
        /// 修改时间
        /// </summary>
        public DateTime UpdatedAt { get; set; } = DateTime.UtcNow;

        /// <summary>
        /// 创建人导航属性
        /// </summary>
        [ForeignKey(nameof(CreatedBy))]
        public User? Creator { get; set; }
        
        /// <summary>
        /// 修改人导航属性
        /// </summary>
        [ForeignKey(nameof(UpdatedBy))]
        public User? Updater { get; set; }
        
        /// <summary>
        /// 租户导航属性
        /// </summary>
        public Tenant? Tenant { get; set; }
        
        /// <summary>
        /// 父页面导航属性
        /// </summary>
        [ForeignKey(nameof(ParentId))]
        public Page? Parent { get; set; }
        
        /// <summary>
        /// 模板导航属性
        /// </summary>
        [ForeignKey(nameof(TemplateId))]
        public Template? Template { get; set; }
        
        /// <summary>
        /// 子页面集合
        /// </summary>
        [InverseProperty(nameof(Parent))]
        public ICollection<Page> Children { get; set; } = new List<Page>();
    }
}
